package org.vegsan.calculator;

import java.util.ArrayList;

/**
 * @author Alex
 * @version 1
 */
public class Calculator {

	private String currentValue = "0";
	private boolean isItFirstDegit = true;
	private Operators previousOperator = Operators.NULL;
	private double firstOperand;
	private double secondOperand;
	private ArrayList <Operation> listOfOperations = new ArrayList<Operation>();
	
public String parseValue(String value){
		if (value.contains(".")){
			char ch = value.charAt(value.length()-1);
			if ((ch == '.') || (ch == '0')){
				value = parseValue(value.substring(0, value.length()-1));
			} 
				
		 
		}
		return value;
	}
	
	
	public String getCurrentValue() {
		return currentValue;
	}

	
	public void setCurrentValue(String currentValue) {
		this.currentValue = currentValue;
	}
	

	
	public String press(Buttons button){
		switch (button) {
			case BUTTON_N0: {
				if (isItFirstDegit){
					currentValue = "0";
//					isItFirstDegit = false;
				}	
			else {
				currentValue += "0";
			}
			}
			break;
			
			case BUTTON_N1: {
				if (isItFirstDegit){
					currentValue = "1";
					isItFirstDegit = false;
				}	
			else {
				currentValue += "1";
			}
			}
			break;
			
			case BUTTON_N2: {
				if (isItFirstDegit){
					currentValue = "2";
					isItFirstDegit = false;
				}	
			else {
				currentValue += "2";
			}
			}
			break;
			
			case BUTTON_N3: {
				if (isItFirstDegit){
					currentValue = "3";
					isItFirstDegit = false;
				}	
			else {
				currentValue += "3";
			}
			} 
			break;
			
			case BUTTON_N4: {
				if (isItFirstDegit){
					currentValue = "4";
					isItFirstDegit = false;
				}	
			else {
				currentValue += "4";
			}
			}
			break;
			
			case BUTTON_N5: {
				if (isItFirstDegit){
					currentValue = "5";
					isItFirstDegit = false;
				}	
			else {
				currentValue += "5";
			}
			}
			break;
			
			case BUTTON_N6: {
				if (isItFirstDegit){
					currentValue = "6";
					isItFirstDegit = false;
				}	
			else {
				currentValue += "6";
			}
			}
			break;
			
			case BUTTON_N7: {
				if (isItFirstDegit){
					currentValue = "7";
					isItFirstDegit = false;
				}	
			else {
				currentValue += "7";
			}
			}
			break;

			case BUTTON_N8: {
				if (isItFirstDegit){
					currentValue = "8";
					isItFirstDegit = false;
				}	
			else {
				currentValue += "8";
			}
			}
			break;

			case BUTTON_N9: {
				if (isItFirstDegit){
					currentValue = "9";
					isItFirstDegit = false;
				}	
			else {
				currentValue += "9";
			}
			}
			break;
			
			case BUTTON_DOT: {
				if (isItFirstDegit){
					currentValue = "0.";
					isItFirstDegit = false;
				}	
			else {
				if (!currentValue.contains("."))
				currentValue += ".";
			}
			}
			break;
			
			case BUTTON_PLUS: {
				currentValue = parseValue(getCurrentValue());
				
				currentValue = executeOperation(Double.parseDouble(currentValue), Operators.PLUS);
				
				currentValue = parseValue(getCurrentValue());
				isItFirstDegit = true;
			}
			break;

			case BUTTON_MINUS: {
				currentValue = parseValue(getCurrentValue());
				
				currentValue = executeOperation(Double.parseDouble(currentValue), Operators.MINUS);
				
				currentValue = parseValue(getCurrentValue());
				isItFirstDegit = true;
			}
			break;

			case BUTTON_MULTIPLAY: {
				currentValue = parseValue(getCurrentValue());
				
				currentValue = executeOperation(Double.parseDouble(currentValue), Operators.MULTIPLAY);
				
				currentValue = parseValue(getCurrentValue());
				isItFirstDegit = true;
			}
			break;

			case BUTTON_DIVIDE: {
				currentValue = parseValue(getCurrentValue());
				
				currentValue = executeOperation(Double.parseDouble(currentValue), Operators.DIVIDE);
				
				currentValue = parseValue(getCurrentValue());
				isItFirstDegit = true;
			}
			break;

			case BUTTON_EQUAL: {
				currentValue = parseValue(getCurrentValue());
				
				currentValue = executeOperation(Double.parseDouble(currentValue), Operators.EQUAL);
				
				currentValue = parseValue(getCurrentValue());
				isItFirstDegit = true;
			}
			break;
			
			case BUTTON_CLEAN: {
				currentValue = parseValue(getCurrentValue());
				
				currentValue = executeOperation(Double.parseDouble(currentValue), Operators.NULL);
				
				isItFirstDegit = true;
			}
			break;
			
		}
		return currentValue;
	}


	public String executeOperation(double anOperand, Operators anOperator){
		
		if (anOperator.equals(Operators.NULL)) {
			firstOperand = anOperand;
			previousOperator = anOperator;
			return "0";
		} 
		
//		if (!anOperator.equals(Operators.EQUAL)) {
			secondOperand = anOperand;
//		} 
		
		Operation op = null;
		
		switch (previousOperator){
		
			case NULL: {
				firstOperand = anOperand;
				previousOperator = anOperator;
				return String.valueOf(anOperand);
			}
			
			case PLUS: {
				op = new Plus(firstOperand,secondOperand); 
				break;
			}
			case MINUS: {
				op = new Minus(firstOperand,secondOperand); 
				break;
			}
			case MULTIPLAY: { op = 
					new Multiplay(firstOperand,secondOperand); 
					break;  
			}
			case DIVIDE: { 
				op = new Divide(firstOperand, secondOperand); 
				break;
			}
				
		}
		
		listOfOperations.add(op);
		firstOperand = op.getResult();
		switch (anOperator) {
			default: previousOperator = anOperator; 
		}
		return String.valueOf(firstOperand);
	}
}
	

